using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

using SqlMiM.Properties;
using Microsoft.Win32;
using System.Globalization;


using System.Windows.Controls.Primitives;
using SqlMiM.ViewModel;
using SqlMiM.Common.Commands;
using SqlMiM.Common;
using SqlMiM.Common.Themes;
using SqlMiM.Views;
using System.Data.SqlClient;
using SqlMiM.Common.Model;


namespace SqlMiM
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class MainWindow : System.Windows.Window
    {
        //-------------------------------------------------------------------
        //
        //  Constructors
        //
        //-------------------------------------------------------------------

        #region Constructors

        public MainWindow()
        {
            InitializeComponent();

            // initialize commands
            CommandBinding binding = null;

            binding = new CommandBinding(CommonCommands.CloseTabCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.CloseTab_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.CloseTab_CanExecute);
            this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.F4, ModifierKeys.Control, "Ctrl+F4")));
            this.CommandBindings.Add(binding);


            binding = new CommandBinding(CommonCommands.ExitCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Exit_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Exit_CanExecute);
            //this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.O, ModifierKeys.Control, "Ctrl+O")));
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.OptionsCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Options_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Options_CanExecute);
            this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.O, ModifierKeys.Control, "Ctrl+O")));
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.NewConnectionCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.NewConnection_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.NewConnection_CanExecute);
            this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.N, ModifierKeys.Control, "Ctrl+N")));
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.AboutCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.About_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.About_CanExecute);
            this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.F12, ModifierKeys.None, "F12")));
            this.CommandBindings.Add(binding);


            #region Edits Commands
            // Edit Commands 
            // We have an unusual situation here where we need to handle Copy/Paste/etc. from the menu
            // separately from the built in keyboard commands that you have in control itself.  This
            // is because of some difficulty in getting commands to go accross the WPF/WinForms barrier.
            // One artifact of this is the fact that we want to create the commands without InputGestures
            // because the WinForms controls will handle the keyboards stuff--so this is for Menu only.

            binding = new CommandBinding(CommonCommands.CopyCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Copy_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Copy_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.CutCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Cut_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Cut_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.PasteCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Paste_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Paste_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.DeleteCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Delete_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Delete_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.UndoCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Undo_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Undo_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.RedoCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Redo_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Redo_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.FindCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Find_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Find_CanExecute);
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.FindNextCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.FindNext_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.FindNext_CanExecute);
            this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.F3, ModifierKeys.None, "F3")));
            this.CommandBindings.Add(binding);

            binding = new CommandBinding(CommonCommands.ReplaceCommand);
            binding.Executed += new ExecutedRoutedEventHandler(this.Replace_Executed);
            binding.CanExecute += new CanExecuteRoutedEventHandler(this.Replace_CanExecute);
            this.InputBindings.Add(new InputBinding(binding.Command, new KeyGesture(Key.H, ModifierKeys.Control, "F3")));
            this.CommandBindings.Add(binding);

            #endregion Edits Commands



            this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);



        }

        private void This_Loaded(object sender, RoutedEventArgs e)
        {
            this.Activate();
            this.InvalidateVisual();
        }

        private void This_Unloaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }


        void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
        }

        #endregion


        //-------------------------------------------------------------------
        //
        //  Commands
        //
        //-------------------------------------------------------------------


        #region ExitCommand


        void Exit_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
                Application.Current.Shutdown();
            }
        }

        void Exit_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region OptionsCommand

        void Options_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
                OptionsView optionsView = new OptionsView();
                optionsView.Owner = this;
                optionsView.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                optionsView.ShowDialog();
            }
        }

        void Options_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region NewConnectionCommand

        void NewConnection_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
                LoginWindow login = new LoginWindow();
                login.Owner = this;
                login.WindowStartupLocation = WindowStartupLocation.CenterOwner;

                SqlConnectionStringBuilder sqlConnectionStringBuilder = null;
                Server server = null;

                bool? isComplete = login.ShowDialog(out sqlConnectionStringBuilder, out server);

                if (isComplete.HasValue && isComplete.Value)
                {
                    // Set Current Server and SqlConnectionStringBuilder;
                    Context.Current.SetConnectionAndServer(sqlConnectionStringBuilder, server);

                    foreach (var p in PluginContext.Current.CurrentPlugins)
                    {
                        //p.Plugin.Deactivate();
                        //System.Threading.Thread.Sleep(1000);
                        p.Plugin.Refresh();

                    }
                }
            }
        }

        void NewConnection_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion


        #region AboutCommand

        void About_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
                AboutView view = new AboutView();
                view.Owner = this;
                view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                view.ShowDialog();
            }
        }

        void About_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region CloseTabCommand


        private void CloseTab_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {

                PluginViewModel tView = args.Parameter as PluginViewModel;

                if (tView == null)
                    tView = PluginContext.Current.ActivePlugin;

                if (tView == null)
                    return;

                int index = this.DocumentsView.CurrentPlugins.IndexOf(tView);

                // Get Active Plugin
                if (index > 0 && this.DocumentsView.CurrentPlugins.Count >= 2)
                    this.DocumentsView.ActivePlugin = this.DocumentsView.CurrentPlugins[index - 1];
                else
                    this.DocumentsView.ActivePlugin = null;
                
                // Call Deactivate on current Plugin
                tView.Plugin.Deactivate();

                // Remove Plugin from DocumentsView
                this.DocumentsView.CurrentPlugins.Remove(tView);


            }
        }

        void CloseTab_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }


        #endregion



        #region Edits Commands

        #region PasteCommand


        void Paste_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Paste_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        #endregion

        #region CopyCommand


        void Copy_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Copy_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        #endregion

        #region CutCommand


        void Cut_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Cut_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        #endregion

        #region DeleteCommand


        void Delete_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Delete_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        #endregion

        #region RedoCommand


        void Redo_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Redo_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region UndoCommand


        void Undo_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Undo_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region FindCommand


        void Find_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Find_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region FindNextCommand


        void FindNext_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
                //Plugin findPlugin = this.PluginView.GetFindPlugin();
                //if (findPlugin.Root is Find)
                //{
                //    string findText = (findPlugin.Root as Find).FindText.Text;
                //}
            }
        }

        void FindNext_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #region ReplaceCommand


        void Replace_Executed(object sender, ExecutedRoutedEventArgs args)
        {
            if (sender == this)
            {
            }
        }

        void Replace_CanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (sender == this)
            {
                args.CanExecute = true;
            }
        }

        #endregion

        #endregion Edits Commands

    }

    public class WindowTitleConverter : IMultiValueConverter
    {
        #region IValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length == 2)
            {
                if (values[0] is string && values[1] is bool)
                {

                    string title = (string)values[0];
                    if ((bool)values[1]) title = title + "*";
                    title = title + " - SqlMiM";

                    return title;
                }
            }

            return null;

            //if (value is XamlDocument)
            //{

            //    switch ((string)parameter)
            //    {
            //        case "Filename":
            //            name = values[1] + ", " + values[0];
            //            break;
            //        case "NeedsSave":
            //        default:
            //            name = values[0] + " " + values[1];
            //            break;
            //    }

            //    XamlDocument document = (XamlDocument)value;

            //    string title = document.Filename;
            //    if (document.NeedsSave) title = title + "*";
            //    title = title + " - SqlMiM";

            //    return title;
            //}
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {

            return null;
        }

        #endregion
    }
}